home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / SpriteWorld 2.0 ƒ / SpriteWorld Examples / Simple / Simple.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-10-06  |  8.1 KB  |  334 lines  |  [TEXT/KAHL]

  1. ///--------------------------------------------------------------------------------------
  2. // Simple.c
  3. ///--------------------------------------------------------------------------------------
  4.  
  5. #include <SWIncludes.h>            // Automatically include all SpriteWorld headers
  6. #include <SWGameUtils.h>
  7. #include <SWFPSReport.h>
  8.  
  9. #include "SWApplication.h"
  10. #include "Simple.h"
  11.  
  12.  
  13. #define kFullScreenWindow        false        // Set to true if you want a window larger than 640x480 
  14. #define kInterlacedMode            false        // You can use interlacing even with non-scrolling animations
  15. #define kSyncToVBL                false        // Syncs the animation to the VBL
  16. #define kWorldRectInset            0            // Makes the SpriteWorld smaller than the window.
  17. #define kMaxFPS                    60            // Keep the animation from going too fast
  18.  
  19. #define kSpriteCIconResID        128
  20. #define kNumSpriteFrames        1
  21.  
  22. #define kNumberOfSprites        6
  23. #define kSpriteMoveTime            0
  24. #define kBaseMoveDelta            1
  25.  
  26.  
  27.  
  28. ///--------------------------------------------------------------------------------------
  29. // main
  30. ///--------------------------------------------------------------------------------------
  31.  
  32. void main(void)
  33. {
  34.     WindowPtr    myWindowP = NULL;
  35.     Rect            windRect;
  36.     RgnHandle    mBarUpdateRgn;
  37.     
  38.     
  39.     Initialize(kNumberOfMoreMastersCalls);
  40.  
  41.     if (!SWHasSystem7())
  42.         CantRunOnThisMachine();
  43.  
  44.  
  45.         // Create and set up window
  46.     myWindowP = GetNewCWindow(kWindowResID, NULL, (WindowPtr)-1L);
  47.  
  48.     if (myWindowP != NULL)
  49.     {
  50.         if (kFullScreenWindow == true)
  51.         {
  52.             SizeWindow(myWindowP, qd.screenBits.bounds.right, 
  53.                             qd.screenBits.bounds.bottom, false);
  54.             MoveWindow(myWindowP, 0, 0, false);
  55.         }
  56.         else
  57.         {
  58.             MoveWindow(myWindowP, 0, 0, false);
  59.             windRect = myWindowP->portRect;
  60.             
  61.                 // Make sure the window fits on the screen
  62.             if (windRect.bottom > qd.screenBits.bounds.bottom ||
  63.                  windRect.right > qd.screenBits.bounds.right)
  64.             {
  65.                     // Make it smaller so it fits on the screen
  66.                 SizeWindow(myWindowP, qd.screenBits.bounds.right, 
  67.                                 qd.screenBits.bounds.bottom, false);
  68.                 MoveWindow(myWindowP, 0, 0, false);
  69.             }
  70.             else
  71.             {
  72.                     // Center window in screen
  73.                 CenterRect(&windRect, &qd.screenBits.bounds);
  74.                 MoveWindow(myWindowP, windRect.left, windRect.top, false);
  75.             }
  76.         }
  77.         
  78.         ShowWindow(myWindowP);
  79.         SetPort(myWindowP);
  80.         mBarUpdateRgn = HideMenuBar(myWindowP); // Must be done *after* showing window!
  81.         EraseRgn(mBarUpdateRgn);
  82.         
  83.         if (kInterlacedMode)
  84.             PaintRect(&myWindowP->portRect);    // Blacken window for Interlaced mode
  85.     }
  86.     else
  87.         CantFindResource();
  88.  
  89.     
  90.     PerformSimpleAnimation((CWindowPtr)myWindowP);
  91. }
  92.  
  93.  
  94. ///--------------------------------------------------------------------------------------
  95. // PerformSimpleAnimation
  96. ///--------------------------------------------------------------------------------------
  97.  
  98. void PerformSimpleAnimation(CWindowPtr srcWindowP)
  99. {
  100.     OSErr                err;
  101.     PixPatHandle        pixPatH;
  102.     
  103.     SpriteWorldPtr        spriteWorldP;
  104.     SpriteLayerPtr        spriteLayerP;
  105.     SpritePtr            simpleSpriteArray[kNumberOfSprites];
  106.     SpritePtr            simpleSpriteP;
  107.     
  108.     Rect                moveBoundsRect;
  109.     Rect                worldRect;
  110.     short                spriteNum;
  111.     unsigned long        frames;
  112.     
  113.  
  114.     SetCursor(*GetCursor(watchCursor));
  115.  
  116.  
  117.     //
  118.     // STEP #1: Initialize the sprite world package
  119.     //
  120.  
  121.     err = SWEnterSpriteWorld();
  122.     FatalError(err);
  123.  
  124.  
  125.     //
  126.     // STEP #2: Create the various pieces that we need
  127.     //
  128.     
  129.     
  130.     worldRect = srcWindowP->portRect;
  131.     InsetRect(&worldRect, kWorldRectInset, kWorldRectInset);
  132.     
  133.         // Create the sprite world
  134.     err = SWCreateSpriteWorldFromWindow(&spriteWorldP, srcWindowP, &worldRect, NULL);
  135.     FatalError(err);
  136.  
  137.  
  138.         // Create the sprite layer
  139.     err = SWCreateSpriteLayer(&spriteLayerP);
  140.     FatalError(err);
  141.  
  142.  
  143.         // Create the first sprite
  144.     err = SWCreateSpriteFromCicnResource(spriteWorldP, &simpleSpriteP, NULL, 
  145.             kSpriteCIconResID, kNumSpriteFrames, kFatMask);
  146.     FatalError(err);
  147.     
  148.     simpleSpriteArray[0] = simpleSpriteP;
  149.  
  150.         // clone the rest of the sprites off the first one
  151.     for (spriteNum = 1; spriteNum < kNumberOfSprites; spriteNum++)
  152.     {
  153.         err = SWCloneSprite(simpleSpriteP, simpleSpriteArray + spriteNum, NULL);
  154.         FatalError(err);
  155.     }
  156.  
  157.  
  158.     //
  159.     // STEP #3: put the pieces together (must be done BEFORE the sprite world is locked!)
  160.     //
  161.  
  162.     for (spriteNum = 0; spriteNum < kNumberOfSprites; spriteNum++)
  163.     {
  164.             // add the sprite to the layer
  165.         SWAddSprite(spriteLayerP, simpleSpriteArray[spriteNum]);
  166.     }
  167.  
  168.         // add the layer to the world
  169.     SWAddSpriteLayer(spriteWorldP, spriteLayerP);
  170.  
  171.  
  172.     //
  173.     // STEP #4: set things up for the animation
  174.     // (can be done before or after locking the SpriteWorld)
  175.     //
  176.  
  177.         // calculate the movement boundary rectangle
  178.     moveBoundsRect = spriteWorldP->backRect;
  179.  
  180.     for (spriteNum = 0; spriteNum < kNumberOfSprites; spriteNum++)
  181.     {
  182.         simpleSpriteP = simpleSpriteArray[spriteNum];
  183.  
  184.             // set up the sprite
  185.         SWSetSpriteMoveBounds(simpleSpriteP, &moveBoundsRect);
  186.         SWSetSpriteMoveTime(simpleSpriteP, kSpriteMoveTime);
  187.         SWSetSpriteMoveProc(simpleSpriteP, BallSpriteMoveProc);
  188.         SWSetSpriteMoveDelta(simpleSpriteP, 
  189.             kBaseMoveDelta+spriteNum, kBaseMoveDelta+spriteNum);
  190.  
  191.             // set the sprite’s initial location
  192.         SWSetSpriteLocation(simpleSpriteP, spriteNum * 60, spriteNum * 60);
  193.     }
  194.     
  195.     
  196.     //
  197.     // Use BlitPixie for the various drawProcs.
  198.     //
  199.  
  200.     if (spriteWorldP->pixelDepth == 8)
  201.     {
  202.         SWCompileSprite(simpleSpriteArray[0]);
  203.         
  204.         if (kInterlacedMode)
  205.         {
  206.             SWSetSpriteWorldScreenDrawProc(spriteWorldP, BP8BitInterlacedRectDrawProc);
  207.             SWSetSpriteWorldOffscreenDrawProc(spriteWorldP, BP8BitInterlacedRectDrawProc);
  208.         }
  209.         else
  210.         {
  211.             SWSetSpriteWorldScreenDrawProc(spriteWorldP, BlitPixie8BitRectDrawProc);
  212.             SWSetSpriteWorldOffscreenDrawProc(spriteWorldP, BlitPixie8BitRectDrawProc);
  213.         }
  214.         
  215.         for (spriteNum = 0; spriteNum < kNumberOfSprites; spriteNum++)
  216.         {
  217.             SWSetSpriteDrawProc(simpleSpriteArray[spriteNum], CompiledSprite8BitDrawProc);
  218.         }
  219.     }
  220.     else    // Not in 256 colors
  221.     {
  222. #if !SW_PPC        // no BlitPixieAllBit in PPC compile
  223.         if (kInterlacedMode)
  224.         {
  225.             SWSetSpriteWorldScreenDrawProc(spriteWorldP, BPAllBitInterlacedRectDrawProc);
  226.             SWSetSpriteWorldOffscreenDrawProc(spriteWorldP, BPAllBitInterlacedRectDrawProc);
  227.         }
  228.         else
  229.         {
  230.             SWSetSpriteWorldScreenDrawProc(spriteWorldP, BlitPixieAllBitRectDrawProc);
  231.             SWSetSpriteWorldOffscreenDrawProc(spriteWorldP, BlitPixieAllBitRectDrawProc);
  232.         }
  233.         
  234.         for (spriteNum = 0; spriteNum < kNumberOfSprites; spriteNum++)
  235.         {
  236.             simpleSpriteP = simpleSpriteArray[spriteNum];
  237.             
  238.             if (kInterlacedMode)
  239.                 SWSetSpriteDrawProc(simpleSpriteP, BPAllBitInterlacedMaskDrawProc);
  240.             else
  241.                 SWSetSpriteDrawProc(simpleSpriteP, BlitPixieAllBitMaskDrawProc);
  242.         }
  243. #endif
  244.     }
  245.  
  246.  
  247.     //
  248.     // STEP #5: Lock the sprite world.        !!! VERY IMPORTANT !!!
  249.     //
  250.  
  251.     SWLockSpriteWorld(spriteWorldP);
  252.  
  253.  
  254.     //
  255.     // SETP #6: Draw a nice background. Must be done *after* locking the SpriteWorld!
  256.     //
  257.  
  258.     SWSetPortToBackground(spriteWorldP);
  259.     
  260.     if (spriteWorldP->pixelDepth == 1)
  261.         pixPatH = GetPixPat(129);        // B&W dithered background
  262.     else
  263.         pixPatH = GetPixPat(128);        // Color
  264.     
  265.     if (pixPatH != NULL)
  266.     {
  267.         FillCRect(&moveBoundsRect, pixPatH);
  268.         DisposePixPat(pixPatH);
  269.     }
  270.  
  271.     SWSetPortToWindow(spriteWorldP);
  272.  
  273.     
  274.     SWSetSpriteWorldMaxFPS(spriteWorldP, kMaxFPS);
  275.     SWSyncSpriteWorldToVBL(spriteWorldP, kSyncToVBL);
  276.     
  277.     SetCursor(&qd.arrow);
  278.     HideCursor();
  279.     
  280.         // Make sure CopyBits doesn't try to colorize things
  281.     ForeColor(blackColor);
  282.     BackColor(whiteColor);
  283.     
  284.     
  285.     //
  286.     // STEP #7: run the animation
  287.     //
  288.     
  289.     
  290.     SWUpdateSpriteWorld(spriteWorldP, true);
  291.     
  292.     frames = 0;
  293.     StartTimer();    // For FPS report later
  294.  
  295.     while (!Button())
  296.     {
  297.         SWProcessSpriteWorld(spriteWorldP);
  298.         SWAnimateSpriteWorld(spriteWorldP);    
  299.         
  300.         if (spriteWorldP->frameHasOccured)
  301.             frames++;        
  302.     }
  303.     
  304.     ShowMenuBar((WindowPtr)srcWindowP);
  305.     ShowResults(frames);    // Show FPS report
  306.     
  307.  
  308.     //
  309.     // STEP #8: Clean up
  310.     //
  311.  
  312.     SWUnlockSpriteWorld(spriteWorldP);
  313.     SWDisposeSpriteWorld(spriteWorldP);
  314.     SWExitSpriteWorld();
  315.     
  316.     FlushEvents(everyEvent, 0);
  317.     ShowCursor();
  318. }
  319.  
  320.  
  321. ///--------------------------------------------------------------------------------------
  322. //  BallSpriteMoveProc
  323. ///--------------------------------------------------------------------------------------
  324.  
  325.  
  326. void    BallSpriteMoveProc(SpritePtr ballSpriteP)
  327. {    
  328.     SWOffsetSprite(ballSpriteP, ballSpriteP->horizMoveDelta, ballSpriteP->vertMoveDelta);
  329.     
  330.     SWBounceSprite(ballSpriteP);
  331. //    SWWrapSprite(ballSpriteP);
  332. }
  333.  
  334.